/**
 * $RCSfile$
 * $Revision$
 * $Date$
 * $Source$
 * $Author$
 */
package pl.edu.agh.loganalizator.view;

import java.awt.Button;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

import pl.edu.agh.loganalizator.model.Column;
import pl.edu.agh.loganalizator.model.ColumnModel;
import pl.edu.agh.loganalizator.model.Rule;
import pl.edu.agh.loganalizator.model.RuleModel;
import sun.awt.HorizBagLayout;

@SuppressWarnings("serial")
public class AddRuleDialogFrame extends JFrame implements ActionListener {

  private JPanel jPanel;
  private RuleModel ruleModel;
  private ColumnModel columnModel;
  private RuleInterface ruleInterface;
  private Rule rule;
  private int state;
  private static final int RULE_PERFORM = 1;
  private static final int COLUMN_PERFORM = 2;
  private Column column;

  private class ObjButton extends Button {
    private Object object;

    public ObjButton(String label) throws HeadlessException {
      super(label);
    }

    public void setObject(Object object) {
      this.object = object;
    }

    public Object getObject() {
      return object;
    }
  };

  private AddRuleDialogFrame() {
    super("Dialog");
    this.setSize(250, 250);
    this.setLayout(new HorizBagLayout());
    this.setVisible(true);
    jPanel = new JPanel();
    jPanel.setLayout(new HorizBagLayout());
    this.add(jPanel);
    pack();
  }

  public AddRuleDialogFrame(RuleModel ruleModel, ColumnModel columnModel, RuleInterface ruleInterface) {
    this();
    this.ruleInterface = ruleInterface;
    this.ruleModel = ruleModel;
    this.columnModel = columnModel;
    createColumnButton(columnModel.getAvailableColumnCollection());

  }

  private void createRuleButton(ArrayList<Rule> arrayRule) {
    jPanel.removeAll();
    for (Rule rule : arrayRule) {
      ObjButton buttonRule = new ObjButton(rule.getName());
      jPanel.add(buttonRule);
      buttonRule.setObject(rule);
      buttonRule.addActionListener(this);
    }
    repaint();
    pack();
  }

  private void createColumnButton(ArrayList<Column> arrayColumn) {
    jPanel.removeAll();
    for (Column column : arrayColumn) {
      ObjButton buttonColumn = new ObjButton(column.getName());
      jPanel.add(buttonColumn);
      buttonColumn.setObject(column);
      buttonColumn.addActionListener(this);
    }
    repaint();
    pack();
  }

  public Rule getRule() {
    return rule;
  }

  @Override
  public void actionPerformed(ActionEvent action) {
    if (action.getSource() instanceof ObjButton) {
      ObjButton objButton = (ObjButton)action.getSource();
      if (objButton.getObject() instanceof Rule) {
        try {
          rule = (Rule)ruleModel.getRuleCless((Rule)objButton.getObject()).newInstance();
          rule.setColumn(column);
        }
        catch (Exception e) {
          System.out.println(e);
          // TODO: handle exception
        }

        jPanel.removeAll();
        jPanel.add(new RuleSetter(rule, this));
        jPanel.repaint();
        repaint();
        pack();
      }
      else if (objButton.getObject() instanceof Column) {
        column = (Column)objButton.getObject();
        createRuleButton(ruleModel.getAvailableRuleCollection());
      }
      else {
      }

    }
  }

  public void setRule(Rule rule) {
    this.removeAll();
    setVisible(false);
    ruleInterface.addRule(rule);
  }

}